#include <iostream>
#include <sys/socket.h>
#include <unistd.h>
#include <string>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
using namespace std;
class Sock
{
public:
    Sock(int sockfd, uint16_t port, string ip)
        : _sockfd(sockfd), _port(port), _ip(ip)
    {
    }

    bool InitSock()
    {
        _sockfd = socket(AF_INET, SOCK_DGRAM, 0);
        if (_sockfd == -1)
        {
            cerr << "socket fail!" << endl;
            return false;
        }
        cout << "create socket success!" << _sockfd << endl;

        struct sockaddr_in local;
        memset(&local, '0', sizeof(local));
        local.sin_family = AF_INET;
        local.sin_port = htons(_port);
        local.sin_addr.s_addr = inet_addr(_ip.c_str());

        if (bind(_sockfd, (struct sockaddr *)&local, sizeof(sockaddr)) < 0)
        {
            cerr << "bind fail!" << _sockfd << endl;
            return false;
        }

        cout << "bind success!" << _sockfd << endl;
        return true;
    }

    ~Sock()
    {
        if (_sockfd >= 0)
            close(_sockfd);
    }

private:
    int _sockfd;
    uint16_t _port;
    string _ip;
};

class Udp_Server
{
public:
    Udp_Server(uint16_t port, string ip)
        : _sockfd(-1), _port(port), _ip(ip)
    {}

    bool InitSock()
    {
        _sockfd = socket(AF_INET, SOCK_DGRAM, 0);
        if (_sockfd == -1)
        {
            cerr << "socket fail!" << endl;
            return false;
        }
        cout << "create socket success!" << _sockfd << endl;

        struct sockaddr_in local;
        memset(&local, '0', sizeof(local));
        local.sin_family = AF_INET;
        local.sin_port = htons(_port);
        local.sin_addr.s_addr = INADDR_ANY;

        if (bind(_sockfd, (struct sockaddr *)&local, sizeof(sockaddr)) < 0)
        {
            cerr << "bind fail!" << _sockfd << endl;
            return false;
        }

        cout << "bind success!" << _sockfd << endl;
        return true;
    }

    bool Start()
    {
#define SIZE 128
        char buffer[SIZE];
        for (;;)
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int ret = recvfrom(_sockfd, buffer, sizeof(buffer)-1, 0, (struct sockaddr *)&peer, &len);
            if (ret > 0)
            {
                buffer[SIZE] = '\0';
                int port = ntohs(peer.sin_port);
                string ip = inet_ntoa(peer.sin_addr);
                cout << ip << ": " << port << "# " << buffer <<endl;
            }
            else
            {
                cerr << "recvfrom fail!" << endl;
                return false;
            }

            std::string echo_msg = "server get!->";
		    echo_msg += buffer;
		    sendto(_sockfd, echo_msg.c_str(), echo_msg.size(), 0, (struct sockaddr*)&peer, len);
        }
    }

private:
    int _sockfd;
    uint16_t _port;
    string _ip;
};



// int main()
// {
//     Sock* sock = new Sock();
//     sock->InitSock();
//     return 0;
// }

int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        cerr << "传入参数不足!" << endl;
        return 1;
    }

    string ip = "127.0.0.1";
    int port = atoi(argv[1]);
    Udp_Server* svr = new Udp_Server(port, ip);
    svr->InitSock();
    svr->Start();
    return 0;
}

